<?php
header("content-type:text/html;charset=utf-8");

include_once "0.1conn.php"; //连接数据库

session_start();
$shop_id = $_SESSION['user']['shop_id'];
if ($_POST['number'] == '') {
    echo json_encode(array());
    exit();
}
if ($_POST['goodid'] == '') {
    $good_id = '%';
} else {
    $good_id = $_POST['goodid'];
}
if ($_POST['number'] == '') {
    $number = '%';
} else {
    $number = $_POST['number'];
}
if ($_POST['style'] == '') {
    $style = '%';
} else {
    $style = $_POST['style'];
}
if ($_POST['color'] == '') {
    $color = '%';
} else {
    $color = $_POST['color'];
}
if ($_POST['size'] == '') {
    $size = '%';
} else {
    $size = $_POST['size'];
}
$query = "SELECT * FROM clothes WHERE good_id LIKE '$good_id' AND style LIKE '$style' AND color LIKE '$color' AND size LIKE '$size'";
$result = mysqli_query($conn, $query);
$arr = array();
if (mysqli_num_rows($result)) {
    $ifsuccess = true;
    //显示查询结果
    while ($row = mysqli_fetch_assoc($result)) {
        $arr[] = $row;
    }
    mysqli_free_result($result);
} else {
    if ($good_id != '') {
        $query = "SELECT * FROM lack_record WHERE good_id = '$good_id' AND shop_id = '$shop_id' ";
        $result = mysqli_query($conn, $query);
        if (mysqli_num_rows($result)) {
            $query = "UPDATE lack_record SET lack_num = lack_num+$number WHERE good_id = '$good_id' AND shop_id = '$shop_id'";
            mysqli_query($conn, $query);
            /*
                    $ifsuccess = true;
                    echo json_encode($ifsuccess);//代表写入缺货记录成功
                    */
        } else {
            $query = "INSERT INTO lack_record (good_id,lack_num,shop_id)
                    values ('$good_id','$number','$shop_id')";
            mysqli_query($conn, $query);
            /*
                    $ifsuccess = true;
                    echo json_encode($ifsuccess);//代表写入缺货记录成功
                    */
        }
    }
    /*else {
                $ifsuccess = false;
                echo json_encode($ifsuccess);//代表写入缺货记录失败，因为没有输入商品编号
            }*/
}
echo json_encode($arr);

mysqli_close($conn);
